package com.qwc.blog.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.crypto.digest.DigestUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qwc.blog.common.constant.CommonConstant;
import com.qwc.blog.common.constant.ConfigConstant;
import com.qwc.blog.config.InitProperties;
import com.qwc.blog.entity.SysConfig;
import com.qwc.blog.entity.dto.PwdUpdateDto;
import com.qwc.blog.mapper.SysConfigMapper;
import com.qwc.blog.service.ISysConfigService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import java.util.stream.Collectors;

/**
 * <p>
 * 配置表 服务实现类
 * </p>
 *
 * @author qwc
 * @since 2022-01-24
 */
@Slf4j
@Service
public class SysConfigServiceImpl extends ServiceImpl<SysConfigMapper, SysConfig> implements ISysConfigService {

    @Autowired
    private SysConfigMapper configMapper;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void initConfig(InitProperties initProperties) {
        Map<String, Object> map = BeanUtil.beanToMap(initProperties);
        Set<Map.Entry<String, Object>> entries = map.entrySet();
        for (Map.Entry<String, Object> entry : entries) {
            config(entry);
        }
    }

    @Override
    public Map<String, String> configMap(String name) {
        List<SysConfig> sysConfigs = configMapper.selectList(Wrappers.<SysConfig>lambdaQuery()
                .eq(SysConfig::getName, name)
                .eq(SysConfig::getStatus, CommonConstant.ABLE));
        return sysConfigs.stream().collect(Collectors.toMap(SysConfig::getConfigKey,
                SysConfig::getConfigValue,
                (v1, v2) -> v1));
    }

    /**
     * 配置基础配置
     *
     * @param entry
     */
    private void config(Map.Entry<String, Object> entry) {
        Map<String, Object> map = BeanUtil.beanToMap(entry.getValue());
        for (Map.Entry<String, Object> configEntry : map.entrySet()) {
            SysConfig config = configMapper.selectOne(Wrappers.<SysConfig>lambdaQuery()
                    .eq(SysConfig::getName, entry.getKey())
                    .eq(SysConfig::getConfigKey, configEntry.getKey()));
            if (Objects.isNull(config)) {
                insertConfig(configEntry, entry);
            }
        }
    }

    /**
     * 保存配置
     *
     * @param configEntry
     * @param entry
     */
    private void insertConfig(Map.Entry<String, Object> configEntry, Map.Entry<String, Object> entry) {
        SysConfig config = new SysConfig();
        config.setName(entry.getKey());
        config.setConfigKey(configEntry.getKey());
        config.setConfigValue(encryptPwd(configEntry));
        config.setCreateTime(new Date());
        config.setStatus(CommonConstant.ABLE);
        config.setType(CommonConstant.ADMIN);
        config.setUpdateTime(new Date());
        configMapper.insert(config);
    }

    /**
     * 加密密码
     *
     * @param configEntry
     * @return
     */
    private String encryptPwd(Map.Entry<String, Object> configEntry) {
        String value = String.valueOf(configEntry.getValue());
        if (configEntry.getKey().equals(ConfigConstant.LOGIN_PASSWORD)) {
            return DigestUtil.bcrypt(value);
        }
        return value;
    }

}
